﻿using System;
using System.Linq;
using Ones.ServiceHumanResourceManagement;

namespace Ones.SysAdmin
{
    public partial class HrMgmtDivPage : System.Web.UI.Page
    {
        private ServiceHumanResourceManagementClient c = null;
        private Division div = null;
        private string itemId = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["cmd"] != null)
                {
                    fillData();

                    if (Session["cmd"].ToString().Equals("delete"))
                    {
                        txtName.Enabled = false;
                        txtDescr.Enabled = false;
                    }
                }
            }
        }

        public void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceHumanResourceManagementClient("hrWsHttpBinding");
                div = new Division();

                div.divName = txtName.Text;
                div.divDescription = txtDescr.Text;

                if (c.addDivision(div))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/SysAdmin/HrListDivJobPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceHumanResourceManagementClient("hrWsHttpBinding");
                div = new Division();

                div.divId = Session["id"].ToString();
                div.divName = txtName.Text;
                div.divDescription = txtDescr.Text;

                if (c.updateDivision(div))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/SysAdmin/HrListDivJobPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceHumanResourceManagementClient("hrWsHttpBinding");
                itemId = Session["id"].ToString();

                if (c.deleteDivision(itemId))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/SysAdmin/HrListDivJobPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnCancel_Click(object sender, EventArgs e)
        {
            Session.Remove("cmd");
            Response.Redirect("~/SysAdmin/HrListDivJobPage.aspx");
        }

        private void fillData()
        {
            c = new ServiceHumanResourceManagementClient("hrBasicHttpBinding");
            
            itemId = Session["id"].ToString();
            div = c.searchDivisionById(itemId);

            txtName.Text = div.divName;
            txtDescr.Text = div.divDescription;

            c.Close();
        }
    }
}